package com.xdhl.fu.fi.runner;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdhl.fu.common.core.constant.flightinfo.FlightInfoConstants;
import com.xdhl.fu.fi.service.IFlightAirportInfoService;
import com.xdhl.fu.flight.info.api.domain.FlightAirportInfo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 数据同步至Redis
 *
 * @author 朱力维
 * @time 2022/6/3 19:39
 */
@Component
public class LoadAirportsHotToCacheAndESRunner implements CommandLineRunner {

    @Resource
    private IFlightAirportInfoService flightAirportInfoService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void run(String... args) throws Exception {
        stringRedisTemplate.delete(FlightInfoConstants.HOT_CITY_CACHE_KEY);
        BoundHashOperations<String, String, String> boundHashOperations = stringRedisTemplate.boundHashOps(FlightInfoConstants.HOT_CITY_CACHE_KEY);
        LambdaQueryWrapper<FlightAirportInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FlightAirportInfo::getIsHot, "1");
        List<FlightAirportInfo> list = flightAirportInfoService.list(wrapper);
        for (FlightAirportInfo flightAirportInfo : list) {
            boundHashOperations.put(flightAirportInfo.getCityCode(), flightAirportInfo.getIsHot());
            boundHashOperations.put(flightAirportInfo.getCityName(), flightAirportInfo.getIsHot());
        }
    }
}
